﻿using System;
using System.IO;
using System.Text;
using System.ComponentModel;
using FlashCards.Dto;
using System.ComponentModel.DataAnnotations;

namespace FlashCards.SL.FlashCardCreator.ViewModels.Utils
{
    public class TextContentViewModel : BaseViewModel, IContentViewModel
    {
        private FlashcardPart _part;

        public TextContentViewModel(FlashcardPart part)
        {
            _part = part;

            if(part.Data != null)
                SetData(part.Data);

            PropertyChanged += TextChanged;
        }

        private string _text;
        public string Text
        {
            get { return _text; }
            set 
            {
                _text = value; OnPropertyChanged("Text"); 
            }
        }

        public byte [] GetData()
        {
            return Encoding.UTF8.GetBytes(Text);
        }

        public void SetData(byte [] data)
        {
            Text = Encoding.UTF8.GetString(data, 0, data.Length);
        }


        void TextChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Text")
            {
                _part.Data = GetData();
            }
        }
    }
}
